/*
 * Modelibra
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package communication.wicket.app;

import java.io.File;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.modelibra.util.Log4jConfigurator;
import org.modelibra.util.PropertiesLocator;
import org.modelibra.util.PathLocator;
import org.mortbay.jetty.Server;

/**
 * Jetty server startup class. See start.properties for a location of the Jetty
 * server configuration file.
 * 
 * Based on the code in the Wicket Examples.
 */
public class Start {

	private static Log log = LogFactory.getLog(Start.class);

	private static final String START_PROPERTIES_FILE_NAME = "Start.properties";

	private static final String CLASS_DIRECTORY_KEY = "class";

	private static final String CONFIG_DIRECTORY_KEY = "config";

	private static final String JETTY_CONFIG_KEY = "jetty";

	private static final String LOG_CONFIG_KEY = "log";

	private static String findJettyConfigPath() {
		String jettyConfigPath = null;
		Class referenceClass = Start.class;
		try {
			PropertiesLocator propertiesLocator = new PropertiesLocator(
					referenceClass, START_PROPERTIES_FILE_NAME);
			String classesDirName = propertiesLocator
					.getProperty(CLASS_DIRECTORY_KEY);
			String configDirName = propertiesLocator
					.getProperty(CONFIG_DIRECTORY_KEY);
			String jettyConfig = propertiesLocator
					.getProperty(JETTY_CONFIG_KEY);
			String log4jConfig = propertiesLocator.getProperty(LOG_CONFIG_KEY);

			PathLocator pathLocator = new PathLocator();
			String log4jConfigPath = pathLocator.findAbsolutePath(
					referenceClass, classesDirName, configDirName
							+ File.separator + log4jConfig);
			Log4jConfigurator log4jConfigurator = new Log4jConfigurator(
					log4jConfigPath);
			log4jConfigurator.configure();

			jettyConfigPath = pathLocator.findAbsolutePath(referenceClass,
					classesDirName, configDirName + File.separator
							+ jettyConfig);
		} catch (Exception e) {
			log.error("Jetty server config load error: " + e.getMessage());
		}
		return jettyConfigPath;
	}

	public static void main(String[] args) {
		Server jettyServer = null;
		try {
			URL jettyConfig = new URL("file:" + findJettyConfigPath());
			if (jettyConfig == null) {
				log.fatal("Unable to locate the Jetty configuration.");
			} else {
				jettyServer = new Server(jettyConfig);
				jettyServer.start();
			}
		} catch (Exception e) {
			log.fatal("Could not start the Jetty server: " + e);
			if (jettyServer != null) {
				try {
					jettyServer.stop();
				} catch (InterruptedException e1) {
					log.fatal("Unable to stop the Jetty server: " + e1);
				}
			}
		}

	}

}
